<?php
//
//	file: includes/acp/acp_attach_quota_def.php
//	author: ptirhiik
//	begin: 24/02/2006
//	version: 1.6.2 - 09/02/2007
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	based on : attachment_mod copyright (c) 2002 Meik Sievertsen (acydburn)
//

if ( !defined('IN_PHPBB') )
{
	die('Hack attempt');
}

$cp_title = 'Manage_quotas';
$cp_title_explain = 'Manage_quotas_explain';

class attach_quota_def
{
	var $requester;
	var $parms;
	var $data;

	function attach_quota_def($requester, $parms)
	{
		$this->requester = $requester;
		$this->parms = empty($parms) ? array() : $parms;
		$this->data = array();
	}

	function process()
	{
		if ( $this->init() )
		{
			$this->check();
			$this->validate();
			$this->display();
			return true;
		}
		return false;
	}

	function init()
	{
		global $db;

		$this->data = array();
		$sql = 'SELECT quota_limit_id, quota_desc, quota_limit
					FROM ' . ATT_QUOTA_LIMITS_TABLE . '
					ORDER BY quota_limit DESC';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		while ( $row = $db->sql_fetchrow($result) )
		{
			$this->data[ intval($row['quota_limit_id']) ] = array(
				'quota_desc' => $row['quota_desc'],
				'quota_limit' => intval($row['quota_limit']),
			);
		}
		$db->sql_freeresult($result);
		return true;
	}

	function check()
	{
	}

	function validate()
	{
		global $error, $error_msg;
		global $db, $user, $config;

		$units = array();
		size_get_units(0, $units);

		$done = false;
		if ( ($quota_limit_id = _read('delete', TYPE_INT)) && isset($this->data[$quota_limit_id]) )
		{
			// delete dependancies
			$sql = 'DELETE FROM ' . ATT_QUOTA_TABLE . '
						WHERE quota_limit_id = ' . intval($quota_limit_id);
			$db->sql_query($sql, false, __LINE__, __FILE__);

			// delete the quota def
			$sql = 'DELETE FROM ' . ATT_QUOTA_LIMITS_TABLE . '
						WHERE quota_limit_id = ' . intval($quota_limit_id);
			$db->sql_query($sql, false, __LINE__, __FILE__);

			$done = true;
		}
		else if ( _button('update') && !empty($this->data) )
		{
			$quota_ids = get_var('quota_ids', array(0));
			$quota_ids = empty($quota_ids) ? array() : array_flip($quota_ids);
			$quota_desc_list = get_var('quota_desc_list', array(''));
			$filesize_list = get_var('max_filesize_list', array(0));
			$unit_list = get_var('unit_list', array(''));
			foreach ( $this->data as $quota_limit_id => $data )
			{
				if ( isset($quota_ids[$quota_limit_id]) )
				{
					$idx = intval($quota_ids[$quota_limit_id]);
					if ( isset($quota_desc_list[$idx]) && isset($filesize_list[$idx]) && isset($unit_list[$idx]) && isset($units[ intval($unit_list[$idx]) ]) )
					{
						$fields = array(
							'quota_desc' => trim($quota_desc_list[$idx]),
							'quota_limit' => intval($filesize_list[$idx]) * intval($unit_list[$idx]),
						);
						if ( $fields != $data )
						{
							$sql = 'UPDATE ' . ATT_QUOTA_LIMITS_TABLE . '
										SET ' . $db->sql_fields('update', $fields) . '
										WHERE quota_limit_id = ' . intval($quota_limit_id);
							$db->sql_query($sql, false, __LINE__, __FILE__);
							$done = true;
						}
					}
				}
			}
		}
		else if ( _button('create') )
		{
			$form_desc = _read('add_quota_desc', TYPE_NO_HTML);
			$form_size = _read('add_max_filesize', TYPE_INT);
			$form_unit = _read('add_unit', TYPE_INT);
			if ( !empty($form_desc) && isset($units[$form_unit]) )
			{
				$fields = array(
					'quota_desc' => $form_desc,
					'quota_limit' => $form_size * $form_unit,
				);
				$sql = 'INSERT INTO ' . ATT_QUOTA_LIMITS_TABLE . '
							(' . $db->sql_fields('fields', $fields) . ') VALUES(' . $db->sql_fields('values', $fields) . ')';
				$db->sql_query($sql, false, __LINE__, __FILE__);
				$done = true;
			}
		}
		if ( $done )
		{
			message_return('Attach_config_updated', 'Click_return_attach_config', $config->url($this->requester, $this->parms, true));
		}
	}

	function display()
	{
		global $template, $user, $config, $db;

		// detail
		if ( !empty($this->data) )
		{
			foreach ( $this->data as $quota_limit_id => $row )
			{
				$units = array();
				$unit = size_get_units(intval($row['quota_limit']), $units);
				$template->assign_block_vars('limit_row', array(
					'QUOTA_ID' => $quota_limit_id,
					'QUOTA_NAME' => $row['quota_desc'],
					'MAX_FILESIZE' => round(intval($row['quota_limit']) / $unit),

					'U_DELETE' => $config->url($this->requester, $this->parms + array('delete' => $quota_limit_id), true),
					'U_VIEW' => $config->url($this->requester, $this->parms + array('view' => $quota_limit_id), true),
				));
				$this->attach_dump_units('limit_row', $unit, $units);
			}
		}

		// header
		$units = array();
		$unit = size_get_units(intval($config->globals['mod_attachment_CH']['config']->data['max_filesize']), $units);
		$template->assign_vars(array(
			'L_VIEW' => $user->lang('View'),
			'I_VIEW' => $user->img('cmd_details'),
			'L_DELETE' => $user->lang('Delete'),
			'I_DELETE' => $user->img('cmd_mini_delete'),
			'L_UPDATE' => $user->lang('Submit'),
			'I_UPDATE' => $user->img('cmd_submit'),
			'S_UPDATE' => $user->lang('cmd_submit'),
			'L_CREATE' => $user->lang('Add_new'),
			'I_CREATE' => $user->img('cmd_create'),
			'S_CREATE' => $user->lang('cmd_create'),

			'L_DESCRIPTION' => $user->lang('Description'),
			'L_SIZE' => $user->lang('Max_filesize_attach'),
			'L_ACTION' => $user->lang('Action'),

			'L_ADD_NEW' => $user->lang('Add_new'),
			'MAX_FILESIZE' => round(intval($config->globals['mod_attachment_CH']['config']->data['max_filesize']) / $unit),
		));
		$template->set_switch('accesskey_update', isset($user->global_lang['cmd_submit']));
		$template->set_switch('accesskey_create', isset($user->global_lang['cmd_create']));
		$this->attach_dump_units('', $unit, $units);

		// report
		if ( ($quota_id = _read('view', TYPE_INT)) && isset($this->data[$quota_id]) )
		{
			$template->set_switch('switch_quota_limit_desc');
			$template->assign_vars(array(
				'L_QUOTA_LIMIT_DESC' => $user->lang($this->data[$quota_id]['quota_desc']),
				'L_ASSIGNED_USERS' => $user->lang('Assigned_users'),
				'L_ASSIGNED_GROUPS' => $user->lang('Assigned_groups'),
				'L_UPLOAD_QUOTA' => $user->lang('Upload_quota'),
				'L_PM_QUOTA' => $user->lang('Pm_quota'),
			));

			// get users data
			$quota_cells = array(QUOTA_UPLOAD_LIMIT => 'users_upload_row', QUOTA_PM_LIMIT => 'users_pm_row');
			$sql = 'SELECT u.user_id, u.username, q.quota_type
						FROM ' . ATT_QUOTA_TABLE . ' q, ' . GROUPS_TABLE . ' g, ' . USERS_TABLE . ' u
						WHERE q.quota_limit_id = ' . intval($quota_id) . '
							AND q.group_id = g.group_id
							AND u.user_id = g.group_user_id
						ORDER BY u.username';
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			while ( $row = $db->sql_fetchrow($result) )
			{
				if ( isset($quota_cells[ intval($row['quota_type']) ]) )
				{
					$template->assign_block_vars($quota_cells[ intval($row['quota_type']) ], array(
						'USER_ID' => $row['user_id'],
						'USERNAME' => $row['username'],
					));
				}
			}
			$db->sql_freeresult($result);

			// get groups data
			$quota_cells = array(QUOTA_UPLOAD_LIMIT => 'groups_upload_row', QUOTA_PM_LIMIT => 'groups_pm_row');
			$sql = 'SELECT q.group_id, g.group_name, g.group_status, q.quota_type
						FROM ' . ATT_QUOTA_TABLE . ' q, ' . GROUPS_TABLE . ' g
						WHERE q.quota_limit_id = ' . intval($quota_id) . '
							AND q.group_id = g.group_id
							AND g.group_user_id = 0
						ORDER BY g.group_status DESC, g.group_name';
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			while ( $row = $db->sql_fetchrow($result) )
			{
				if ( isset($quota_cells[ intval($row['quota_type']) ]) )
				{
					$template->assign_block_vars($quota_cells[ intval($row['quota_type']) ], array(
						'GROUP_ID' => $row['group_id'],
						'GROUPNAME' => $row['group_status'] >= GROUP_SYSTEM ? $user->lang($row['group_name']) : $row['group_name'],
					));
				}
			}
			$db->sql_freeresult($result);
		}
	}

	function attach_dump_units($tpl_switch, $unit, &$units)
	{
		global $template, $user;

		if ( !empty($tpl_switch) )
		{
			$tpl_switch .= '.';
		}
		foreach ( $units as $unit_id => $unit_desc )
		{
			$template->assign_block_vars($tpl_switch . 'unit', array(
				'UNIT_ID' => $unit_id,
				'UNIT_DESC' => $user->lang($unit_desc),
			));
			$template->set_switch($tpl_switch . 'unit.selected', $unit_id == $unit);
		}
	}
}

// instantiate the form
$parms = array(
	'mode' => $menu_id,
	'sub' => $subm_id == $menu_id ? '' : $subm_id,
	'ctx' => $ctx_id == $subm_id ? '' : $ctx_id,
);
$form = new attach_quota_def($cp_requester, $parms);
$form->process();
$template->assign_block_vars('cp_content', array('BOX' => $template->include_file('acp/acp_attach_quota_def_body.tpl')));
$template->set_filenames(array('body' => 'cp_generic.tpl'));

?>